package com.amir.common.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.amir.common.R;


public class CustomToolBar extends LinearLayout implements View.OnClickListener {
    private final Context mContext;
    private TextView mTvTitle;
    private ImageView mIvRight;
    private TextView mTvRight;
    private TextView mTvRight2;
    private View mLine;
    private LinearLayout mRlRight;
    private RelativeLayout mRlBack;
    private LinearLayout tootbar;
    public EditText mEtSearch;
    public TextView mTvSearchClose;
    public LinearLayout mLlSearch;
    private TextView mTvLefttext;
    private RelativeLayout rl_boobar;
    private ImageView mIvRight2;

    public CustomToolBar(Context context) {
        super(context);
        this.mContext = context;
        initView(context, null, 0);
    }

    public CustomToolBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView(context, attrs, 0);
    }

    public CustomToolBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView(context, attrs, defStyleAttr);
    }

    @SuppressLint("ResourceType")
    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        LayoutInflater.from(context).inflate(R.layout.custom_toolbar, this);

        ImageView mIvBack = findViewById(R.id.iv_back);
        mTvTitle = findViewById(R.id.tv_title);
        mTvRight = findViewById(R.id.tv_right);
        mTvRight2 = findViewById(R.id.tv_right2);
        mIvRight = findViewById(R.id.iv_right);
        mIvRight2 = findViewById(R.id.iv_right2);
        mLine = findViewById(R.id.line);
        mRlBack = findViewById(R.id.rl_back);
        mRlRight = findViewById(R.id.rl_right);
        tootbar = findViewById(R.id.root);
        mTvLefttext = findViewById(R.id.tv_lefttext);
        mRlBack.setOnClickListener(this);
        mRlRight.setOnClickListener(this);
        mIvRight.setOnClickListener(this);
        mIvRight2.setOnClickListener(this);
        mTvLefttext.setOnClickListener(this);
        this.mEtSearch = (EditText) findViewById(R.id.et_search);
        this.mTvSearchClose = (TextView) findViewById(R.id.tv_search_close);
        this.mLlSearch = (LinearLayout) findViewById(R.id.ll_search);
        this.rl_boobar = findViewById(R.id.rl_boobar);
        this.mTvSearchClose.setOnClickListener(this);
        this.mLlSearch.setVisibility(GONE);
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomToolBar);
            int title_text_color = typedArray.getColor(R.styleable.CustomToolBar_title_text_color, 0xff000000);
            String title_text = typedArray.getString(R.styleable.CustomToolBar_title_text);
            String left_text = typedArray.getString(R.styleable.CustomToolBar_left_text);
            int left_textcolor = typedArray.getColor(R.styleable.CustomToolBar_left_textcolor, 0Xff000000);
            float title_size = typedArray.getDimension(R.styleable.CustomToolBar_title_text_size, 18);
            int back_src = typedArray.getResourceId(R.styleable.CustomToolBar_back_src, R.drawable.back);
            int right_src = typedArray.getResourceId(R.styleable.CustomToolBar_right_src, 0);
            int right_src2 = typedArray.getResourceId(R.styleable.CustomToolBar_right_src2, 0);
            int right_src2_v = typedArray.getInteger(R.styleable.CustomToolBar_right_src2_visibility, View.GONE);
            int back_v = typedArray.getInteger(R.styleable.CustomToolBar_back_visibility, View.VISIBLE);
            int right_title_v = typedArray.getInteger(R.styleable.CustomToolBar_right_title_visibility, View.VISIBLE);
            String right_title = typedArray.getString(R.styleable.CustomToolBar_right_title);
            String right_title2 = typedArray.getString(R.styleable.CustomToolBar_right_title2);
            int right_title_color = typedArray.getColor(R.styleable.CustomToolBar_right_title_color, 0xff000000);
            int bg_color = typedArray.getColor(R.styleable.CustomToolBar_bg_color, 0xffffffff);
            float right_title_size = typedArray.getDimension(R.styleable.CustomToolBar_right_title_size, 14);
            int right_title2_color = typedArray.getColor(R.styleable.CustomToolBar_right_title2_color, 0xff000000);
            float right_title2_size = typedArray.getDimension(R.styleable.CustomToolBar_right_title2_size, 14);

            int back_visible = typedArray.getInteger(R.styleable.CustomToolBar_back_image, View.VISIBLE);
            float line_height = typedArray.getDimension(R.styleable.CustomToolBar_line_height, DensityUtil.dip2px(mContext, 1));
            int line_color = typedArray.getColor(R.styleable.CustomToolBar_line_color, 0x00000000);
            boolean isbackclick = typedArray.getBoolean(R.styleable.CustomToolBar_backclick, true);

            int title_bgisVi = typedArray.getInteger(R.styleable.CustomToolBar_title_image, View.GONE);
            if (!isbackclick) {
                mRlBack.setOnClickListener(null);
            }
            mIvBack.setVisibility(back_visible);
            if (back_src == 0) {
                mIvBack.setVisibility(GONE);

            } else {
                mRlBack.setVisibility(VISIBLE);
            }
            if (!TextUtils.isEmpty(left_text)) {
                mTvLefttext.setVisibility(VISIBLE);
            } else {
                mTvLefttext.setVisibility(GONE);

            }
            mIvBack.setVisibility(back_v);
            mIvBack.setImageResource(back_src);
            mTvTitle.setText(title_text);
            mTvLefttext.setText(left_text);
            mTvLefttext.setTextColor(left_textcolor);
            mTvTitle.setTextColor(title_text_color);
            mTvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, title_size);
            mTvRight.setText(right_title);
            mTvRight.setTextColor(right_title_color);
            mTvRight.setTextSize(TypedValue.COMPLEX_UNIT_SP, right_title_size);

            mTvRight2.setText(right_title2);
            mTvRight2.setTextColor(right_title2_color);
            mTvRight2.setTextSize(TypedValue.COMPLEX_UNIT_SP, right_title2_size);


            mIvRight.setImageResource(right_src);

            mIvRight2.setVisibility(right_src2_v);

            mIvRight2.setImageResource(right_src2);

            mTvRight.setVisibility(right_title_v);
            mLine.setBackgroundColor(line_color);

            ViewGroup.LayoutParams layoutParams = mLine.getLayoutParams();
            layoutParams.height = (int) line_height;
            mLine.setLayoutParams(layoutParams);
            typedArray.recycle();
            this.setBackgroundColor(bg_color);
        }

        // 判断api版本号是否大于等于21
        if (Build.VERSION.SDK_INT > 23) {
            LayoutParams params = (LayoutParams) tootbar.getLayoutParams();
//            params.topMargin = DensityUtil.dip2px(mContext, BaseContents.TopHeight);
            params.topMargin = getStatusBarHeight();
            tootbar.setLayoutParams(params);
//            this.setBackgroundColor(mContext.getResources().getColor(bg_color));
        }

    }

    /**
     * 根据 ID 来获取 View
     *
     * @param viewId viewID
     * @param <T>    泛型
     * @return 将结果强转为 View 或 View 的子类型
     */
    public <T extends View> T getView(int viewId) {
        // 先从缓存中找，找打的话则直接返回
        // 如果找不到则 findViewById ，再把结果存入缓存中

        View view = findViewById(viewId);

        return (T) view;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.rl_back) {
            if (onLeftClickListener == null) {

                ((Activity) mContext).finish();
            } else {

                onLeftClickListener.onTitleLeft();
            }

        } else if (i == R.id.rl_right) {
            if (onRightClickListener != null) {
                onRightClickListener.onRight();
            }

        } else if (i == R.id.tv_lefttext) {
            if (mOnLeftTextClickListener != null) {
                mOnLeftTextClickListener.onTitleLeftText();
            }

        } else if (i == R.id.tv_search_close) {
            hindSearch();
        } else if (i == R.id.iv_right) {

            if (onRightSrc1ClickListener != null) {
                onRightSrc1ClickListener.onRightSrc1();
            }

        } else if (i == R.id.iv_right2) {
            if (onRightSrc2ClickListener != null) {
                onRightSrc2ClickListener.onRightSrc2();
            }
        }
    }

    public void showSearch() {
        this.mLlSearch.setVisibility(VISIBLE);
        this.rl_boobar.setVisibility(GONE);
        IMEUtil.INSTANCE.showIME(mContext, mEtSearch);

    }

    public void hindSearch() {
        this.mLlSearch.setVisibility(GONE);
        mEtSearch.setText("");
        this.rl_boobar.setVisibility(VISIBLE);

    }

    public void hideNavBar(){
        this.rl_boobar.setVisibility(GONE);
    }

    public void setTitle(String title) {
        mTvTitle.setText(title);
    }

    public void setBackColor(int color) {

        this.setBackgroundColor(color);
    }

    public void setTitle(int titleId) {
        mTvTitle.setText(titleId);
    }


    public void setRightText(String text) {
        mTvRight.setText(text);
    }

    public String getRightText() {

        return mTvRight.getText().toString().trim();
    }

    public boolean isShowSearch() {

        return mLlSearch.getVisibility() == View.VISIBLE;
    }

    public interface OnRightClickListener {
        void onRight();
    }

    public interface OnRightSrc1ClickListener {
        void onRightSrc1();
    }

    public interface OnRightSrc2ClickListener {
        void onRightSrc2();
    }

    public void setRightSrc(int icon) {

        mIvRight.setImageResource(icon);
    }

    public void setRightV(int v) {
        mRlRight.setVisibility(v);
    }

    private OnRightClickListener onRightClickListener;

    public void setOnRightClickListener(OnRightClickListener onRightClickListener) {
        this.onRightClickListener = onRightClickListener;
    }

    private OnRightSrc1ClickListener onRightSrc1ClickListener;

    public void setOnRightSrc1ClickListener(OnRightSrc1ClickListener onRightSrc1ClickListener) {
        this.onRightSrc1ClickListener = onRightSrc1ClickListener;
    }

    private OnRightSrc2ClickListener onRightSrc2ClickListener;

    public void setOnRightSrc2ClickListener(OnRightSrc2ClickListener onRightSrc1ClickListener) {
        this.onRightSrc2ClickListener = onRightSrc1ClickListener;
    }

    public interface OnLeftClickListener {
        void onTitleLeft();
    }

    private OnLeftClickListener onLeftClickListener;

    public void setOnLeftClickListener(OnLeftClickListener onLeftClickListener) {
        this.onLeftClickListener = onLeftClickListener;
    }

    public interface OnLeftTextClickListener {
        void onTitleLeftText();
    }

    private OnLeftTextClickListener mOnLeftTextClickListener;

    public void setOnLeftTextClickListener(OnLeftTextClickListener onLeftTextClickListener) {
        mOnLeftTextClickListener = onLeftTextClickListener;
    }

    /**
     * 利用反射获取状态栏高度
     *
     * @return
     */
    public int getStatusBarHeight() {
        int result = 0;
        //获取状态栏高度的资源id
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    public static
    class ViewHolder {
        public View rootView;


        public ViewHolder(View rootView) {
            this.rootView = rootView;

        }

    }
}

